﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XRpgLibrary;
using XRpgLibrary.TileEngine;
using Anthem.Components;
using XRpgLibrary.SpriteClasses;
using EyesOfTheDragon.Components;
using EyesOfTheDragon.Events;
using XRpgLibrary.Events;
using Microsoft.Xna.Framework.Media;

namespace Anthem.GameScreens
{
    public class GamePlayScreen : BaseGameState
    {
        #region Field Region
        Engine engine = new Engine(32, 32); //Width and height of the tiles in tileset.
        
        Player player;
        Monster monster;
       // map m;
        public static GamePlayScreen Instance;
       // public Game GameRef;
        #endregion
        #region Property Region
        #endregion
        #region Constructor Region
        public GamePlayScreen(Game game, GameStateManager manager)
            : base(game, manager)
        {
         //   player = new Player(game);
            monster = new Monster(game);
            Instance = this;
            //GameRef = game ;
        }
        #endregion      
        #region XNA Method Region
        public override void Initialize()
        {
            base.Initialize();
        }
        protected override void StateChange(object sender, EventArgs e)
        {
            if ((sender as GameStateManager).CurrentState is GamePlayScreen)
            {
                Song _song = GameRef.Content.Load<Song>(@"Music\The House of Leaves");
                MediaPlayer.Play(_song);
                map.LoadFromXml(GameRef);
                HealthMeter.Instance._roundTime = 60*7;
            }
            base.StateChange(sender, e);
        }
        protected override void LoadContent()
        {
            AnimatedSprite sprite;
     //       map.LoadFromXml(GameRef);
           // Texture2D spriteSheet = Game.Content.Load<Texture2D>(
           //     @"PlayerSprites\" +
           //     GameRef.CharacterGeneratorScreen.SelectedGender +
          //      GameRef.CharacterGeneratorScreen.SelectedClass);
            /*
            Texture2D spriteSheet2 = Game.Content.Load<Texture2D>(@"MonsterSprites\monsterWalk");*/
            /*
            Dictionary<AnimationKey, Animation> animations = new Dictionary<AnimationKey, Animation>();
            Animation animation = new Animation(4, 53, 106, 0, 0);
            animations.Add(AnimationKey.Down, animation);
            animation = new Animation(4, 53, 106, 0, 106);
            animations.Add(AnimationKey.Left, animation);
            animation = new Animation(4, 53, 106, 0, 212);
            animations.Add(AnimationKey.Right, animation);
            animation = new Animation(4, 53, 106, 0, 318);
            animations.Add(AnimationKey.Up, animation);
            sprite = new AnimatedSprite(spriteSheet, animations);
            player.Sprite = sprite;
             */
            ////////////
            /*
            Dictionary<AnimationKey, Animation> animations1 = new Dictionary<AnimationKey, Animation>();
            animation = new Animation(4, 64, 128, 0, 128);
            animations1.Add(AnimationKey.Down, animation);
            animation = new Animation(4, 64, 128, 0, 128);
            animations1.Add(AnimationKey.Left, animation);
            animation = new Animation(4, 64, 256, 0, 256);
            animations1.Add(AnimationKey.Right, animation);
            animation = new Animation(3, 32, 32, 0, 96);
            animations1.Add(AnimationKey.Up, animation);
            sprite1 = new AnimatedSprite(spriteSheet2, animations1);
            monster.Monsters.Add(new BaseMonster(sprite1));
             * */
            //////////
            MonsterGenerator.Instance.init(Game);
            UpdateList.Instance.add(PhysicsSimulation.Instance);
            UpdateList.Instance.add(Lamps.Instance);
            UpdateList.Instance.add(HoleTraps.Instance);
            UpdateList.Instance.add(PauseManager.Instance);
            UpdateList.Instance.add(Doors.Instance);
            UpdateList.Instance.add(HealthMeter.Instance);
          ///  DrawList.Instance.add(PauseManager.Instance);
           // PhysicsSimulation.Instance.Update(gameTime);
        //    Doors.Instance.Update(gameTime);
           // Lamps.Instance.Update(gameTime);
           // HoleTraps.Instance.Update(gameTime);
            base.LoadContent();
           // Texture2D tilesetTexture = Game.Content.Load<Texture2D>(@"Tilesets\tileset1");
           
           // Tileset tileset1 = new Tileset(tilesetTexture, 8, 8, 32, 32);
           // tilesetTexture = Game.Content.Load<Texture2D>(@"Tilesets\tileset2");
           // Tileset tileset2 = new Tileset(tilesetTexture, 8, 8, 32, 32);
           // List<Tileset> tilesets = new List<Tileset>();
          //  tilesets.Add(tileset1);
           // tilesets.Add(tileset2);
           // MapLayer layer = new MapLayer(100, 100);
            /*
            for (int y = 0; y < layer.Height; y++)
            {
                for (int x = 0; x < layer.Width; x++)
                {
                    Tile tile = new Tile(0, 0);
                    layer.SetTile(x, y, tile);
                }
            }
            MapLayer splatter = new MapLayer(100, 100);
             * */
            /*Random random = new Random();
            for (int i = 0; i < 100; i++)
            {
                int x = random.Next(0, 100);
                int y = random.Next(0, 100);
                int index = random.Next(2, 14);
                Tile tile = new Tile(index, 0);
                splatter.SetTile(x, y, tile);
            }*/
            /*
            splatter.SetTile(1, 0, new Tile(14, 1));
            splatter.SetTile(2, 0, new Tile(15, 1));
            splatter.SetTile(3, 0, new Tile(16, 1));
            splatter.SetTile(4, 0, new Tile(17, 1));
            splatter.SetTile(5, 0, new Tile(18, 1));
            splatter.SetTile(6, 0, new Tile(19, 1));
            splatter.SetTile(7, 0, new Tile(20, 1));
            splatter.SetTile(8, 0, new Tile(21, 1));
            splatter.SetTile(9, 0, new Tile(22, 1));
            splatter.SetTile(10, 0, new Tile(23, 1));
             */
           // splatter.SetTile(8, 0, new Tile(5, 3));


            /*
            List<MapLayer> mapLayers = new List<MapLayer>();
            mapLayers.Add(layer);
            mapLayers.Add(splatter);
             */
           // map = new TileMap(tilesets, mapLayers);
            //player.init();
            
           // DrawList.Instance.add(sprite1);
        }
        public override void Update(GameTime gameTime)
        {
            if (HealthMeter.Instance._roundTime < 0)
                KillPlayer();
            base.Update(gameTime);
            UpdateList.Instance.Update(gameTime);
            
            DestroyList.Instance.destroyAll();
        }
        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin(
                SpriteSortMode.Deferred,
        BlendState.AlphaBlend,
        SamplerState.PointClamp,
        null,
        null,
        null,
                Player.Instance.Camera.Transformation);
            //map.Draw(GameRef.SpriteBatch, player.Camera);
            map.Instance.Draw(gameTime, GameRef.SpriteBatch); 
            DrawList.Instance.Draw(gameTime, GameRef.SpriteBatch);
         //   SpriteEffects.
            base.Draw(gameTime);
            HealthMeter.Instance.Draw(gameTime, GameRef.SpriteBatch);
            PauseManager.Instance.Draw(gameTime, GameRef.SpriteBatch);
            GameRef.SpriteBatch.End();
        }

        public void KillPlayer()
        {
            Player.Instance.Kill();
            InputHandler.Flush();
            StateManager.PopState();
            StateManager.ChangeState(GameRef.GameOverScreen);
        }
        public void WinGame()
        {
            
            Player.Instance.Kill();
            InputHandler.Flush();
            StateManager.PopState();
            GameOverScreen.won = true;
            StateManager.ChangeState(GameRef.GameOverScreen);
        }
        #endregion
    }
}